#include "CenterWidget.h"
#include "PageRelevancy.h"
#include "item/LabelItem.h"

CenterWidget::CenterWidget(QWidget* parent) : PageElement(parent)
{
	m_mainLayout = new QVBoxLayout(this);
	QLabel* title = new QLabel("center widget");
	m_mainLayout->addWidget(title);
	setLayout(m_mainLayout);
}

void CenterWidget::addItem(QWidget* widget)
{
	m_mainLayout->insertWidget(m_mainLayout->count(), widget);
	PageRelevancy::getInstance()->insertParent(this, widget);
	if (auto item = dynamic_cast<ItemBase*>(widget))
	{
		m_operator.addItem(item);
	}
}

void CenterWidget::comeIn()
{
	m_operator.selectFirstItem();
	PageRelevancy::getInstance()->setFocus(this);
}

void CenterWidget::left()
{
	if (auto item = m_operator.getSelect())
	{
		if (auto widget = dynamic_cast<PageElement*>(item))
		{
			widget->left();
		}
	}
}

void CenterWidget::right()
{
	if (auto item = m_operator.getSelect())
	{
		if (auto widget = dynamic_cast<PageElement*>(item))
		{
			widget->right();
		}
	}
}

void CenterWidget::up()
{
	m_operator.left();
}

void CenterWidget::down()
{
	m_operator.right();
}

void CenterWidget::enter()
{
	if (auto item = m_operator.getSelect())
	{
		if (auto widget = dynamic_cast<PageElement*>(item))
		{
			widget->enter();
		}
	}
}

void CenterWidget::leave()
{
	emit signal_leave();
	m_operator.clearAllItemStat();
}

void CenterWidget::esc()
{
	leave();
}
